package chenji.StrongFossil.Puncher;

import java.util.HashSet;
import java.util.List;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class PuncherManager {
	private HashSet<Puncher> PuncherSet;

	public PuncherManager(HashSet<Puncher> PuncherSet) {
		this.PuncherSet = PuncherSet;
	}

	public boolean isPuncher(ItemStack i) {
		for (Puncher p : PuncherSet) {
			if (p.equals(i)) {
				return true;
			}
		}
		return false;
	}

	public int getNowDurable(String s) {
		String[] StringSet = s.replaceAll("§c⇨", "").split("/");
		System.out.println(StringSet[0]);
		return Integer.valueOf(StringSet[0]);

	}

	public int getMaxDurable(String s) {
		String[] StringSet = s.replaceAll("§c⇨ ", " ").split("/");
		return Integer.valueOf(StringSet[1]);
	}

	public ItemStack updateDurable(ItemStack i, int nowDurable) {
		if (i == null) {
			return i;
		}
		if (!(i.hasItemMeta())) {
			return i;
		}
		if (!(i.getItemMeta().hasLore())) {
			return i;
		}
		int nowLine = 0;
		List<String> lore = i.getItemMeta().getLore();
		for (String s : lore) {
			if (s.startsWith("§c⇨") && s.contains("/")) {
				if (this.getMaxDurable(s) < nowDurable) {
					nowDurable = this.getMaxDurable(s);
				}
				String newDurableLine = "§c⇨" + nowDurable + "/" + this.getMaxDurable(s);
				lore.set(nowLine, newDurableLine);
				break;
			}
			nowLine++;
		}
		ItemMeta m = i.getItemMeta();
		m.setLore(lore);
		i.setItemMeta(m);
		return i;
	}

	public HashSet<Puncher> getPuncherSet() {
		return this.PuncherSet;
	}

	public int getNowDurable(ItemStack item) {
		if (item == null) {
			return 0;
		}
		if (!(item.hasItemMeta())) {
			return 0;
		}
		if (!(item.getItemMeta().hasLore())) {
			return 0;
		}
		List<String> lore = item.getItemMeta().getLore();
		for (String s : lore) {
			if (s.startsWith("§c⇨") && s.contains("/")) {
				return this.getNowDurable(s);
			}
		}
		
		return 0;
	}
}
